package com.doordu.soa.service.web.auth.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.doordu.soa.service.comm.utils.SpringContextHolder;
import com.doordu.soa.service.web.auth.model.TokenModel;
import com.doordu.soa.service.web.auth.service.RedisTokenManager;
import com.doordu.soa.service.web.expection.DataNullException;
import com.doordu.soa.service.web.expection.GzipException;
import com.doordu.soa.service.web.expection.NoPermissionException;
import com.doordu.soa.service.web.expection.OperateTypeException;

public class TokenInterceptor implements HandlerInterceptor {

	protected static final Logger logger = LoggerFactory.getLogger(TokenInterceptor.class);

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj,
			Exception exception) throws Exception {
		logger.debug(">>>TokenInterceptor>>>>>>>在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView exception)
			throws Exception {
		logger.debug(">>>TokenInterceptor>>>>>>>请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		String requestMethod = request.getRequestURI();
		String accessToken = request.getParameter("accessToken");
		RedisTokenManager redisTokenManager = SpringContextHolder.getBean(RedisTokenManager.class);
		if (requestMethod.contains("auth")	|| requestMethod.contains("demo")||(requestMethod.contains("apis") || requestMethod.contains("api-docs"))) {
			TokenModel tokenModel = new TokenModel(accessToken);
			redisTokenManager.checkToken(tokenModel);
			return true;
		}
		if (StringUtils.isBlank(accessToken)) {
			throw new NoPermissionException("没有权限");
		}
		TokenModel tokenModel = new TokenModel(accessToken);
		Integer flag = redisTokenManager.checkToken(tokenModel);
		if (flag.intValue()!=0) {
			throw new NoPermissionException("没有权限");
		}
		if (requestMethod.contains("query") || requestMethod.contains("auth")||requestMethod.contains("uploadFile")) {
			return true;
		}
		String operateType = request.getParameter("operateType");
		if (StringUtils.isBlank(operateType) || !(operateType.equals("1") || operateType.equals("2"))) {
			throw new OperateTypeException("上报数据的同步类型错误");
		}

		String gzip = request.getParameter("gzipFlag");
		if (StringUtils.isBlank(gzip) || !(gzip.equals("0") || gzip.equals("1"))) {
			throw new GzipException("上报数据的压缩类型错误");
		}
		if (StringUtils.isBlank(request.getParameter("data"))) {
			throw new DataNullException("上报数据体data不能为空");
		}

		redisTokenManager.tokenAddExpired_at(accessToken);
		return true;
	}
}